<?php
/**
 * Autor:   Miguel A. Zilli
 * Email:   miguelzilli@gmail.com
 */

class Acoproduccion extends AppModel {
    var $name = 'Acoproduccion';
    var $displayField = 'campania_prod';
    var $recursive='0';
    var $order=array('Acoproduccion.campania_prod'=>'DESC');

    var $validate = array(
        'campania_prod' => array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'fch_prod' => array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'sup_prod' => array(
            array('rule'=>'numeric','message'=>'Solo se permiten números.','allowEmpty'=>false),
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'semillas_prod' => array(
            array('rule'=>'numeric','message'=>'Solo se permiten números.','allowEmpty'=>false),
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'combustible_prod' => array(
            array('rule'=>'numeric','message'=>'Solo se permiten números.','allowEmpty'=>false),
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'producto_id' => array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        )
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'Producto' => array(
            'className' => 'Producto',
            'foreignKey' => 'producto_id'
        ),
        'Productor' => array(
            'className' => 'Productor',
            'foreignKey' => 'productor_id'
        ),
        'Predio' => array(
            'className' => 'Predio',
            'foreignKey' => 'predio_id'
        )
    );

    var $hasMany = array(
        'Agroquimicoxproduccion' => array(
            'className' => 'Agroquimicoxproduccion',
            'foreignKey' => 'acoproduccion_id',
            'dependent' => false
        )
    );

    public function beforeSave($options = array()){
        $this->data['Acoproduccion']['fch_prod'] = $this->formatDate($this->data['Acoproduccion']['fch_prod']);
        if(!empty($this->data['Acoproduccion']['observ_prod'])){
            $this->data['Acoproduccion']['observ_prod']=strtoupper($this->data['Acoproduccion']['observ_prod']);
        }
        return parent::beforeSave($options);
    }

    public function afterFind($result) {
        foreach ($result as $clave => $valor) {
            if (isset($valor['Acoproduccion']['fch_prod'])) {
                $result[$clave]['Acoproduccion']['fch_prod'] = $this->formatoFechatAfterFind($valor['Acoproduccion']['fch_prod']);
            }
        }
        return parent::afterFind($result);
    }

    function formatoFechatAfterFind($fecha) {
        return date('d-m-Y', strtotime($fecha));
    }

    function formatDate($dateToFormat){
        $pattern1 = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/i';
        $pattern2 = '/^([0-9]{4})\/([0-9]{2})\/([0-9]{2})$/i';
        $pattern3 = '/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/i';
        $pattern4 = '/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/i';

        $coincidences = array();

        if(preg_match($pattern1, $dateToFormat)){
            $newDate = $dateToFormat;
        }elseif(preg_match($pattern2, $dateToFormat, $coincidences)){
            $newDate = $coincidences[1] . '-' . $coincidences[2] . '-' . $coincidences[3];
        }elseif(preg_match($pattern3, $dateToFormat, $coincidences)){
            $newDate = $coincidences[3] . '-' . $coincidences[2] . '-' . $coincidences[1];
        }elseif(preg_match($pattern4, $dateToFormat, $coincidences)){
            $newDate = $coincidences[3] . '-' . $coincidences[2] . '-' . $coincidences[1];
        }else{
            $newDate = null;
        }
        return $newDate;
    }
}
?>